package com.srunpass.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * @author srunpass
 * @version Id: 统计重复个数, v 0.1 2024/8/6 17:20 srunpass Exp $
 */
public class 统计重复个数 {
    public static void main(String[] args) {
        统计重复个数 ss = new 统计重复个数();
        int maxRepetitions = ss.getMaxRepetitions("aaa", 20, "aaaaa", 1);
        System.out.println(maxRepetitions);
    }

    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {

        if (n1 == 0) {
            return 0;
        }
        char[] ss1 = s1.toCharArray();
        char[] ss2 = s2.toCharArray();

        Map<Integer,int[]> map = new HashMap<>();
        int s1cnt=0,s2cnt=0;
        int[] first = new int[2];
        int[] second = new int[2];
        int index  =0;
        while (true){
            s1cnt++;
            for (int i = 0; i < ss1.length; i++) {
                char c1 = ss1[i];
                if(c1==ss2[index]){
                    index++;
                    if(index==ss2.length){
                        s2cnt++;
                        index=0;
                    }
                }
            }
            if(s1cnt>=n1){
                return s2cnt/n2;
            }

            if(map.containsKey(index)){
                first = map.get(index);
                second = new int[]{s1cnt-first[0],s2cnt-first[1]};
                break;
            }else {
                map.put(index,new int[]{s1cnt,s2cnt});
            }
        }

        int ans = first[1]+(n1-first[0])/second[0]*second[1];
        int rest = (n1-first[0])%second[0];
        for(int i=0;i<rest;i++){
            for (char c1 : ss1) {
                if (c1 == ss2[index]) {
                    index++;
                    if (index == ss2.length) {
                        ans++;
                        index = 0;
                    }
                }
            }
        }
        return ans/n2;

    }




}
